package com.sk.skplanet.commute.views;

import java.io.File;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sk.skplanet.commute.R;
import com.sk.skplanet.commute.actions.Actions;
import com.sk.skplanet.commute.actions.LoadingTask;
import com.sk.skplanet.commute.controllers.DialogController;
import com.sk.skplanet.commute.vo.Data;

public class MemberLogin extends Activity {

	RelativeLayout layout_memberlogin;
	Button button_memberlogin;
	EditText edittext_memberlogin_email, edittext_memberlogin_password;
	Button button_memberlogin_autologin;
	TextView textview_memberlogin_memberjoin;
	TextView textview_autologin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.memberlogin);

		layout_memberlogin = (RelativeLayout) findViewById(R.id.layout_memberlogin);
		button_memberlogin = (Button) findViewById(R.id.button_memberlogin);
		button_memberlogin.setBackgroundResource(R.drawable.btn_01_nor);
		button_memberlogin.setTextColor(Color.parseColor(Data.textview_pointColor));
		button_memberlogin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				new LoadingTask(v.getContext(), edittext_memberlogin_email.getText().toString(),
						edittext_memberlogin_password.getText().toString(), button_memberlogin_autologin.getTag()
								.equals(1)).execute();

			}
		});
		button_memberlogin.setOnTouchListener(new OnTouchListener() { // 버튼 터치시 이벤트
					public boolean onTouch(View v, MotionEvent event) {
						if (event.getAction() == MotionEvent.ACTION_DOWN) // 버튼을 누르고 있을 때
						v.setBackgroundResource(R.drawable.btn_01_tch);
						if (event.getAction() == MotionEvent.ACTION_UP) { // 버튼에서 손을 떼었을 때
							v.setBackgroundResource(R.drawable.btn_01_nor);
						}
						return false;
					}
				});

		layout_memberlogin.setBackgroundColor(Color.parseColor(Data.layout_pointColor));
		edittext_memberlogin_email = (EditText) findViewById(R.id.edittext_memberlogin_email);
		edittext_memberlogin_email.setBackgroundResource(R.drawable.edit_bg);
		edittext_memberlogin_email.setTextColor(Color.parseColor(Data.textview_pointColor));
		edittext_memberlogin_password = (EditText) findViewById(R.id.edittext_memberlogin_password);
		edittext_memberlogin_password.setBackgroundResource(R.drawable.edit_bg);
		edittext_memberlogin_password.setTextColor(Color.parseColor(Data.textview_pointColor));

		button_memberlogin_autologin = (Button) findViewById(R.id.button_memberlogin_autologin);
		button_memberlogin_autologin.setTag(0);

		button_memberlogin_autologin.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (v.getTag().equals(0)) {
					button_memberlogin_autologin.setBackgroundResource(R.drawable.checkbox_on);
					v.setTag(1);
				} else {
					button_memberlogin_autologin.setBackgroundResource(R.drawable.checkbox_off);
					v.setTag(0);
				}

			}
		});

		textview_autologin = (TextView) findViewById(R.id.textview_autologin);
		textview_autologin.setTextSize(Actions.DPFromPixel(12));
		textview_autologin.setTextColor(Color.parseColor(Data.textview_pointColor));

		textview_memberlogin_memberjoin = (TextView) findViewById(R.id.textview_memberlogin_memberjoin);
		textview_memberlogin_memberjoin.setTextSize(Actions.DPFromPixel(12));
		textview_memberlogin_memberjoin.setTextColor(Color.parseColor(Data.textview_pointColor));
		textview_memberlogin_memberjoin.setOnTouchListener(new OnTouchListener() { // 버튼 터치시 이벤트
					public boolean onTouch(View v, MotionEvent event) {
						if (event.getAction() == MotionEvent.ACTION_DOWN) // 버튼을 누르고 있을 때
						((TextView) v).setTextColor(Color.RED);
						if (event.getAction() == MotionEvent.ACTION_UP) { // 버튼에서 손을 떼었을 때
							((TextView) v).setTextColor(Color.parseColor(Data.textview_pointColor));
						}
						return false;
					}
				});
		textview_memberlogin_memberjoin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				DialogController.createMemberjoinDialog(MemberLogin.this).show();
			}
		});


		File target = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + Data.FilePATH
				+ Data.FileNAME);
		if (!target.exists()) {
			DialogController.createDBdownloadDialog(MemberLogin.this).show();
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.memberlogin, menu);
		return true;
	}

}
